﻿Imports System.Data.OleDb

Public Class EditArticle
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim UserID As String
        Dim MemUser As MembershipUser
        MemUser = Membership.GetUser
        UserID = MemUser.ProviderUserKey.ToString


        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim SqlString As String = "select tblArticles.*, tblCategories.CategoryName from tblArticles inner join tblCategories on (tblArticles.category = tblCategories.ID) where tblArticles.ID=@f1 and tblArticles.UserID = @f2"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            cmd.Parameters.AddWithValue("@f1", UserID)
            oleDbConn.Open()

            Dim dr As OleDbDataReader = cmd.ExecuteReader()

            If dr.HasRows Then
                dr.Read()
                ' If Not IsDBNull(dr("CategoryName")) Then ddlCategory.SelectedValue = dr("CategoryName")
                If Not IsDBNull(dr("Title")) Then txtTitle.Text = dr("Title")
                If Not IsDBNull(dr("Picture")) Then myPicture.ImageUrl = dr("Picture")
                If IsDBNull(dr("Picture")) Then myPicture.ImageUrl = "images\blank.jpg"
                If Not IsDBNull(dr("Details")) Then txtDetails.Text = dr("Details")
            End If
        End If
    End Sub

    Protected Sub btnUpdateArticle_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnUpdateArticle.Click
        Dim UserID As String
        Dim MemUser As MembershipUser
        MemUser = Membership.GetUser
        UserID = MemUser.ProviderUserKey.ToString

        Dim newFileName As String = ""
        Dim SqlString As String

        If fuPicture.HasFile Then
            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid
            newFileName = myGUID.ToString() + ".jpg"
            Dim picLocation = Request.MapPath("pictures") & "/" & newFileName
            fuPicture.SaveAs(picLocation)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        If fuPicture.HasFile Then
            SqlString = "Update tblArticles set Category=@f1,Title=@f2,Picture=@f3,Details=@f4,DateAdded=@f5,UserID=@f6 where ID = @f7"
        Else
            SqlString = "Update tblArticles set Category=@f1,Title=@f2,Picture=@f3,Details=@f4,DateAdded=@f5,UserID=@f6 where ID = @f7"
        End If

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)

        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", ddlCategory.SelectedValue)
        cmd.Parameters.AddWithValue("@f2", txtTitle.Text)

        If fuPicture.HasFile Then
            cmd.Parameters.AddWithValue("@f3", "pictures/" & newFileName)
        Else
            cmd.Parameters.AddWithValue("@f3", "pictures/default.article.jpg")
        End If

        cmd.Parameters.AddWithValue("@f4", txtDetails.Text)
        cmd.Parameters.AddWithValue("@f5", Date.Today)
        cmd.Parameters.AddWithValue("@f6", UserID)
        cmd.Parameters.AddWithValue("@f6", Request.Params("ID"))

        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()

        Response.Redirect("EditArticleConfirmation.aspx")
    End Sub
End Class